(function() {

    var baseTypes = [
        '[object String]',
        '[object Number]',
        '[object Boolean]',
        '[object Undefined]',
        '[object Null]',
        '[object Date]'
    ];

    var Util = {
        colorIndex: 0,
        colorList: [
            '#66CCCC',
            '#666699',
            '#FF99CC',
            '#99CC66',
            '#FF9900',
            '#FFCC00'
        ],
        getColor: function (index) {
            var i = Number(index) % this.colorList.length;
            return this.colorList[i];
        },
        getColorAuto: function () {
            var i = this.colorIndex % this.colorList.length;
            this.colorIndex ++;
            return this.colorList[i];
        },
        getType: function(obj) {
            return Object.prototype.toString.call(obj);
        },
        // 判断是否基础类型
        isBaseType: function(obj) {
            var type = this.getType(obj);
            for(var i in baseTypes) {
                var bType = baseTypes[i];
                if(type == bType) {
                    return true;
                }
            }
            return false;
        },
        isString: function(obj) {
            return this.getType(obj) == '[object String]' ;
        },
        isArray: function(obj) {
            return this.getType(obj) == '[object Array]' ;
        },
        isObject: function(obj) {
            return this.getType(obj) == '[object Object]' ;
        },
        /**
         * 判断数组是否相等
         * @param {Array} array1 
         * @param {Array} array2 
         */
        isArrayEqual: function(array1, array2) {
            if(array1.length == array2.length) {
                for(var i in array1) {
                    if(array1[i] != array2[i]) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        },
        inArray: function(target, array) {
            for(var i in array) {
                var obj = array[i];
                if(target == obj) {
                    return true;
                }
            }
            return false;
        },
        // 克隆对象
        clone: function(obj) {
            return JSON.parse( JSON.stringify(obj) );
        },
        // 仅克隆基础类型属性
        cloneBase: function(obj) {
            var cloneObj = this.clone(obj);
            var newObj = {};
            for(var key in cloneObj) {
                var value = cloneObj[key];
                if(this.isBaseType(value)) {
                    newObj[key] = value;
                }
            }
            return newObj;
        },
        isBlank: function(str) {
            if(str == null || str == '') {
                return true;
            } else {
                return false;
            }
        },
        clearBlank: function(param) {
            for(var key in param) {
                if(this.isBlank(param[key])) {
                    delete param[key]
                }
            }
        },
        deleteIndex: function(arr, index) {
            arr.splice(index, 1);
        },
        arrayDelete: function(arr, val) {
            var index = arr.indexOf(val);
            if (index > -1) {
                arr.splice(index, 1);
            }
        },
        swapArray: function(arr, i1, i2) {
            // console.log(arr);
            var arr2 = [];
            for(var i in arr) {
                if(i == i1) {
                    arr2.push(arr[i2]);
                } else if(i == i2) {
                    arr2.push(arr[i1]);
                } else {
                    arr2.push(arr[i]);
                }
            }
            // console.log(arr2);
            return arr2;
        },
        // 构建毫秒
        buildMs() {
            return this.buildNo('ms');
        },
        buildVinNo(_succ) {
            return this.buildNo('vinNo', _succ);
        },
        build_yyyyMMddHHmmss() {
            return new Date().format('yyyyMMddHHmmss');
        },
        buildLocalNo(type) {
            if('ms' == type) {
                return new Date().getTime();
            }
            return 'unkown';
        },
        buildNo(type, _succ) {
            if('ms' == type) {
                var no = this.buildLocalNo(type);
                return no;
            }
            var param = {
                type: type
            };
            var url = Config.space + 'tool/buildNo';
            $.post(url, param, (result) => {
                // this.data.vinNo = result;
                _succ(result);
            });
        }
    };

    window.Util = Util;

} ());